<?php
namespace app\api\controller;

use app\api\ApiBase;
use think\Db;

class Order extends ApiBase{
	public $order_status_arr = [
		'-1'=>'支付失败',
		'0'=>'待支付',
		'1'=>'已支付',
		'2'=>'已完成',
		'3'=>'取消订单'
	];
	public function __construct(){
        parent::__construct();
    }

    public function index(){
        
    }

    public function addorder(){
    	$data = array();
    	$uid = $this->user_info['id'];//用户id（测试）
        $order_amount = input('order_amount',0);
        $goods_id = input('goods_id',0);
        if(empty($goods_id))$this->json_return(0, "缺少参数", []);

        $goods = db('goods')->where(['id'=>$goods_id])->find();
        if(empty($goods))$this->json_return(0, "商品不存在", []);

    	$data['user_id'] = $this->user_info['id'];
        $data['user_name'] = $this->user_info['display_name'];
    	$data['order_sn'] = date('YmdHis') . rand(10000000,99999999);//系统订单号（测试）
        $data['add_time'] = time();
    	$data['order_amount'] = $goods['goods_price'];//input('order_amount',0);//金额
        $data['pay_name'] = input('pay_id')==1 ? '微信支付':'';
        $data['goods_name'] = $goods['goods_name'];
        $data['goods_id'] = $goods['id'];
        $data['mark'] = '';

        $order_id = db('order_info')->insertGetId($data);//下单
        if(!empty($order_id)){
            $this->get_pay_info($order_id);//生成微信统一下单
        }else{
            $this->json_return(0, "返回数据失败", []);
        }
    }

    //获取支付信息
    public function get_pay_info($order_id=0){
        $payment_info = config('wechat.wxapp');//支付配置
        \think\Loader::import('org.payments.wap.wxpay.Wxpay',EXTEND_PATH);//导入第三方支付类
        $wxpay =  new \Wxpay($payment_info);
        $order_info = \think\Loader::model('OrderInfo')->find(['order_id'=>$order_id]);

        $pay_info = $wxpay->get_payform($order_info,$this->user_info['openid']);
        if(!empty($pay_info)){
            $this->json_return(1, "返回数据成功", $pay_info);
        }else{
            $this->json_return(0, "返回数据失败", []);
        }

    }

}
